package com.example.jl.bethinner.activity;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.jl.bethinner.R;
import com.example.jl.bethinner.base.BaseActivity;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends BaseActivity {
    @ViewInject(id = R.id.image)
    ImageView imageView;
    //上次按下返回键的系统时间
    private long lastBackTime = 0;
    //当前按下返回键的系统时间
    private long currentBackTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FinalActivity.initInjectedView(this);
        //8s之后自动关闭
        //  AutoCloseActivity();


        // 通知栏demo
        //   TestEDI();

        // 延迟跳转
        AutoGo();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //捕获返回键按下的事件
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //获取当前系统时间的毫秒数
            currentBackTime = System.currentTimeMillis();
            //比较上次按下返回键和当前按下返回键的时间差，如果大于2秒，则提示再按一次退出
            if (currentBackTime - lastBackTime > 2 * 1000) {
                Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
                lastBackTime = currentBackTime;
            } else { //如果两次按下的时间差小于2秒，则退出程序
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void AutoCloseActivity() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                finish();
            }
        }, 8000);
    }

    public void AutoWxGo() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                RunWxByPackname(MainActivity.this);
            }
        }, 1000);
        SetImage(this);
    }

    public void AutoGo() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
//                startActivity(new Intent(MainActivity.this, WebViewActivity.class).putExtra("url","http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"));
                startActivity(new Intent(MainActivity.this, ActionBarStudyActivity.class));
            }
        }, 1000);
        SetImage(this);
    }

    public void TestEDI() {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int notificationIcon = R.mipmap.ic_launcher;
//        * Notification noti = new Notification.Builder(mContext)
//                *         .setContentTitle(&quot;New mail from &quot; + sender.toString())
//        *         .setContentText(subject)
//                *         .setSmallIcon(R.drawable.new_mail)
//                *         .setLargeIcon(aBitmap)
//                *         .build();
        Notification notification = new Notification.Builder(this).setContentText("text")
                .setContentTitle("title").setSmallIcon(notificationIcon).setAutoCancel(true).build();
        mNotificationManager.notify(1, notification);
    }

    public void Run(View view) {
        RunWxByPackname(this);
    }


    public void RunWxByPackname(Context context) {
        PackageManager pm = context.getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage("com.tencent.mm");
        if (intent != null) {
            context.startActivity(intent);
            finish();
        } else {
            Toast.makeText(context, "你他妈还没安装微信，点个蛋", Toast.LENGTH_SHORT).show();
        }
    }

    public void SetImage(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            imageView.setImageDrawable(pm.getApplicationIcon("com.tencent.mm"));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}
